// soPdf.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "soPdf.h"
#include "processPdf.h"

//
// Definitions
//
bool    p_proceedWithErrors = false;
bool    p_reverseLandscape = false;
bool    p_cropWhiteSpace = true;
double  p_overlap = 2;
EMode   p_mode = Fit2xWidth;

// Pdf files
soPdfFile   inPdfFile;
soPdfFile   outPdfFile;
#include <vector>
std::vector<soPdfFile*> inPdfFiles;


static int  opterr = 1;     /* if error message should be printed */
static int  optind = 1;     /* index into parent argv vector */
static int  optopt;			/* character checked for validity */
static char *optarg;		/* argument associated with option */

#define	BADCH	            (int)'?'
#define	EMSG	            ""
#define COPY_ARG(_x, _y)    strcpy_s(_x, sizeof(_x), _y)


int getopt(int nargc, char * const * nargv, const char *ostr)
{
	static char *place = EMSG;      /* option letter processing */
	register char *oli;             /* option letter list index */
	char *p;

	if (!*place) {				/* update scanning pointer */
		if (optind >= nargc || *(place = nargv[optind]) != '-') {
			place = EMSG;
			return(EOF);
		}
		if (place[1] && *++place == '-') {	/* found "--" */
			++optind;
			place = EMSG;
			return(EOF);
		}
	}					/* option letter okay? */
	if ((optopt = (int)*place++) == (int)':' ||
	    !(oli = (char*)strchr(ostr, optopt))) {
		/*
		 * if the user didn't specify '-' as an option,
		 * assume it means EOF.
		 */
		if (optopt == (int)'-')
			return(EOF);
		if (!*place)
			++optind;
		if (opterr) {
			if (!(p = strrchr(*nargv, '/')))
				p = *nargv;
			else
				++p;
			(void)fprintf(stderr, "%s: illegal option -- %c\n",
			    p, optopt);
		}
		return(BADCH);
	}
	if (*++oli != ':') {			/* don't need argument */
		optarg = NULL;
		if (!*place)
			++optind;
	}
	else {					/* need an argument */
		if (*place)			/* no white space */
			optarg = place;
		else if (nargc <= ++optind) {	/* no arg */
			place = EMSG;
			if (!(p = strrchr(*nargv, '/')))
				p = *nargv;
			else
				++p;
			if (opterr)
				(void)fprintf(stderr,
				    "%s: option requires an argument -- %c\n",
				    p, optopt);
			return(BADCH);
		}
		else				/* white space */
			optarg = nargv[optind];
		place = EMSG;
		++optind;
	}
	return(optopt);				/* dump back option letter */
}


int
soPdfError(
    fz_error *error
    )
{
    int ctr = 0;
    fz_error *e = error;
    while (error)
    {
        printf("%*sError: %s(%d) : %s() - %s\n", 
            ctr++, "", error->file, error->line, 
            error->func, error->msg);

        error = error->cause;
    }

    fz_droperror(e);
    return 1;
}

#include <exception>

void
throwSoPdfError(fz_error *error)
{
    int ctr = 0;
    fz_error *e = error;
    while (error)
    {
        printf("%*sError: %s(%d) : %s() - %s\n", 
            ctr++, "", error->file, error->line, 
            error->func, error->msg);

        error = error->cause;
    }

    fz_droperror(e);
	throw std::exception("sopdf error");
}


int
soPdfUsage(void)
{
    fprintf(stderr,
        "about: soPdf_lon\n"
        "   author: Navin Pai, soPdf ver " SO_PDF_VER " customized by mumurik\n"
        "usage: \n"
		"   soPdf_lon output_file input_file1 [input_file2 [input_file3...]]\n"
		);

    return 1;
}

int 
_tmain(int argc, _TCHAR* argv[])
{
    if (argc < 2)
        return soPdfUsage();

    initSoPdfFile(&outPdfFile);

    p_overlap = 1;

	COPY_ARG(outPdfFile.fileName, argv[1]);
	for(int i = 2; i < argc; i++)
	{
		soPdfFile *tmp = new soPdfFile();
		initSoPdfFile(tmp);
		COPY_ARG(tmp->fileName, argv[i]);
		inPdfFiles.push_back(tmp);
	}

    
    if (inPdfFiles.size() == 0)
        return soPdfUsage();

    if (outPdfFile.fileName[0] == 0)
        return soPdfUsage();

    // The reverseLandscape works with landscape mode only
    if (! ((p_mode == FitWidth) || 
           (p_mode == Fit2xWidth) || 
           (p_mode == SmartFitWidth)))
        p_reverseLandscape = false;

    printf("\nsoPdf_lon ver " SO_PDF_VER "\n");
    printf("\tA program to reformat pdf file for sony reader\n");
    printf("Output: %s\n\n", outPdfFile.fileName);

    return processPdfFiles(inPdfFiles, &outPdfFile);
}

